<template>
  <div class="app-container">
    <div class="filter-container">
      <p style="display:inline-block;margin-left:2%">商品类型：</p>
      <el-select v-model="depValue" size="small" placeholder="全部" clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in deps" :key="item.exchangeId" :label="item.name" :value="item.catId" />
      </el-select>
      <p style="display:inline-block;margin-left:2%">商品品牌：</p>
      <el-input
        v-model="listQuery.goodsBrand"
        size="small"
        placeholder="商品品牌"
        style="width: 200px;margin-left:2%"
        class="filter-item"
        @keyup.enter.native="handleFilter" />
      <el-button
        size="small"
        class="filter-item"
        type="success"
        icon="el-icon-search"
        style="margin-left:3%"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
          size="small"
          class="filter-item"
          style="margin-right: 10px;float:right"
          type="primary"
          icon="el-icon-edit"
          @click="handleCreate"
        >
          添加
        </el-button>
    </div>

    <el-table
      :key="tableKey"
      :data="list"
      size="small"
      border
      fit
      highlight-current-row
      style="width: 100%;margin-top:1%"
    >
    <el-table-column type="index" label="序号">
    </el-table-column>
      <el-table-column label="商品编码" width="150px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.proCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品" min-width="250px" align="center">
        <template slot-scope="scope" style="display:flex; justify-content:space-around">
          <div style="display:flex; justify-content:space-around;width:100%;height:150px">
            <div style="width:50%;height:150px">
              <img :src="scope.row.imageUrl" style="width:70%;height:150px" >
            </div>
            <div style="width:50%;height:100%;display:flex;flex-direction:column;align-items:center">
              <p style="display:block;width:100%; height: 50%">{{ scope.row.infoName }}</p>
              <p style="display:block;width:100%; height: 50%">{{ scope.row.proTitle }}</p>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品品牌" width="150px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.goodsBrand }}</span>
        </template>
      </el-table-column>
      <el-table-column label="销售价/市场价" width="150px" align="center">
        <template slot-scope="scope">
          <span>销售价：{{ scope.row.sellPrice }}</span><br>
          <span>市场价：{{ scope.row.marketPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="抵扣小康币" width="100px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.eqXkb }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品类型" width="150px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.proCat }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品类型" width="100px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.status==='0'?'未开始':scope.row.status==='1'?'在售中':'已结束' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品数量" width="100px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.exchangeAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="300px"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{ row }">
          <el-button
            size="small"
            align="center"
            type="primary"
            @click="handleUpdate(row)"
          >
            编辑
          </el-button>
          <el-button
            size="small"
            align="center"
            type="danger"
            @click="handleDel(row)"
          >
            删除
          </el-button>
          <el-button
            size="small"
            align="center"
            type="success"
            @click="handleDetails(row)"
          >
            查看兑奖人名单
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
    <el-dialog
    width="60%"
    :title="textMap[dialogStatus]"
    :visible.sync="dialogFormVisible"
    @close="chearData"
    append-to-body>
    <el-form
        ref="dataForm"
        :model="temp"
        label-position="right"
        label-width="240px"
        style="width: 700px; margin-left:150px;"
    >
        <el-form-item label="商品ID(商品查询列表中获取):" prop="proId">
        <el-input style="width:300px" v-model="temp.proId"/>
        <el-button type="primary" style="margin-left:20px;" @click="queryGoods">查询商品</el-button>
        </el-form-item>
        <el-form-item label="商品名称:" v-if="isShow" prop="GoodsName">
          <el-input v-model="GoodsName"/>
        </el-form-item>
        <el-form-item label="商品分类:" v-if="isShow">
          <el-input v-model="temp.proCat"/>
        </el-form-item>
        <el-form-item label="商品编码:" v-if="isShow">
          <el-input v-model="temp.proCode"/>
        </el-form-item>
        <el-form-item label="商品品牌:" v-if="isShow">
          <el-input v-model="temp.goodsBrand"/>
        </el-form-item>
        <el-form-item label="商品规格(不同价格)：" v-if="isShow" prop="proType">
        <el-table :data="data1List" tooltip-effect="dark" ref="tableData" @row-click="singleElection" stripe border style="width:100%">
            <el-table-column prop="prop" label="单选" align="center">
                <template slot-scope="scope">
                <el-radio class="radio" v-model="templateSelection" :label="scope.$index">&nbsp;</el-radio>
                </template>
                <!-- v-model="scope.row.id == transferForm.selectedId" -->
            </el-table-column>
            <el-table-column label="型号" align="center">
                <template slot-scope="scope">
                <span>{{scope.row.proTitle}}</span>
                </template>
            </el-table-column>
            <el-table-column label="销售价格" align="center">
                <template slot-scope="scope">
                <span>{{scope.row.proSellPrice/100}}</span>
                </template>
            </el-table-column>
            <el-table-column label="采购价格" align="center">
                <template slot-scope="scope">
                <span>{{scope.row.purchansePrice/100}}</span>
                </template>
            </el-table-column>
            <el-table-column label="市场价格" align="center">
                <template slot-scope="scope">
                <span>{{scope.row.proMarketPrice/100}}</span>
                </template>
            </el-table-column>
            </el-table>
        </el-form-item>
        <el-form-item label="任务展示图标：" v-if="isShow">
          <el-upload
            class="avatar-uploader"
            action="http://10.0.0.123:10001/uploadImgUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :http-request="customRequest">
            <img style="width:100px;height:100px" v-if="temp.imageUrl" :src="temp.imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="抵扣小康币：">
            <el-input v-model="temp.eqXkb"/>
        </el-form-item>
        <el-form-item label="商品数量：">
            <el-input v-model="temp.exchangeAmount"/>
        </el-form-item>
        <el-form-item label="活动状态：">
            <el-radio v-model="temp.status" label="0">未开始</el-radio>
            <el-radio v-model="temp.status" label="1">在售中</el-radio>
            <el-radio v-model="temp.status" label="2">已结束</el-radio>
        </el-form-item>
        
    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
        type="primary"
        @click="dialogStatus === 'create' ? createData() : updateData()"
        >
        确定
        </el-button>
    </div>
    </el-dialog>
    <el-dialog
    width="60%"
    title="查看兑奖名单"
    :visible.sync="detailsVisible"
    append-to-body>
    <div class="filter-container">
        <p style="display:inline-block;margin-left:2%">兑奖人名字：</p>
      <el-input
        v-model="detailsQuery.nickName"
        size="small"
        placeholder="兑奖人名字"
        style="width: 200px;margin-left:2%"
        class="filter-item"
        @keyup.enter.native="handleFilter1" />
      <p style="display:inline-block;margin-left:2%">性别：</p>
      <el-select v-model="detailsQuery.sex" size="small" placeholder="全部" clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name" :value="item.key"/>
      </el-select>
      <p style="display:inline-block;margin-left:2%">手机号：</p>
      <el-input
        v-model="detailsQuery.userName"
        size="small"
        placeholder="手机号"
        style="width: 200px;margin-left:2%"
        class="filter-item"
        @keyup.enter.native="handleFilter1" />
      <el-button
        size="small"
        class="filter-item"
        type="success"
        icon="el-icon-search"
        style="margin-left:3%"
        @click="handleFilter1"
      >
        搜索
      </el-button>
    </div>
    <el-table
      :data="datalist"
      size="small"
      border
      fit
      highlight-current-row
      style="width: 100%;margin-top:1%"
    >
      <el-table-column label="APP账户" min-width="150px" align="center">
        <template slot-scope="scope" style="display:flex; justify-content:space-around">
          <span>{{ scope.row.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" width="100px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" width="50px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.sex==='MAN'?'男':scope.row.sex==='WOMAN'?'女':'' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="地址" width="200px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.address }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品编码" width="200px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.proCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" width="200px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.proName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="销售价/市场价" width="150px" align="center">
        <template slot-scope="scope">
          <span>市场价：{{ scope.row.marketPrice }}</span><br>
          <span>销售价：{{ scope.row.sellPrice }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total1 > 0"
      :total="total1"
      :page.sync="detailsQuery.pageNum"
      :limit.sync="detailsQuery.pageSize"
      @pagination="handleFilter1"
    />
    <div slot="footer" class="dialog-footer">
        <el-button @click="detailsVisible = false">取 消</el-button>
    </div>
    </el-dialog>
  </div>
</template>

<script>
// 异步请求 delLly
import { getListPage,delProinfo,updataForm,createForm,getDetailsList } from '@/api/shoppingExchange/index'
import Vue from 'vue'
import {
  getSearchList,
} from '@/api/commodity'
import {addImg} from '@/api/headWheel'
import { parseTime } from '@/utils'
import { queryGoods } from '@/api/activityManage'
import Pagination from '@/components/Pagination' // 分页插件

const calendarTypeOptions = [
  { key: 'MAN', display_name: '男' },
  { key: 'WOMAN', display_name: '女' },
]
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})

export default {
  inject:["reload"],
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type]
    },
  },
  data() {
    return {
      value1:'',
      data1List:[],
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      deps:[],
      total1:0,
      datalist:[],
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        proCode:'',
        infoName:''
      },
      detailsQuery:{
        pageNum: 1,
        pageSize: 10,
      },
      templateSelection:'',
      temp:{
        marketPrice:'',
        proTitle:'',
        proPriceId:'',
        weekPrizeId:'',
        sellPrice:'',
        infoName:'',
        imageUrl:''
      },
      isShow:false,
      dialogFormVisible: false,
      detailsVisible:false,
      calendarTypeOptions,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '添加',
      },
      rowId:"",
      delId:'',
      depValue: '',
    }
  },
  created() {
    this.getList()
    this.getSearch()
  },
  methods: {
    // 查询商品分类
    getSearch() {
      this.deps = []
      getSearchList().then(res => {
        this.deps = res.data
      })
    },
    handleAvatarSuccess(res, file) {
        this.temp.imageUrl = URL.createObjectURL(file.raw);
    },
    customRequest (data) {
      const formData = new FormData()
      formData.append('file', data.file)
      this.saveFile(formData)
    },
    saveFile (formData) {
      addImg(formData).then(res=>{
        // this.imageUrl=res.data
        this.$set(this.temp,'imageUrl',res.data.filePath)
        console.log(this.temp.imageUrl)
      })
    },
    chearData(){
      this.isShow=false
    },
    singleElection (row) {
      this.templateSelection = this.data1List.indexOf(row);
      console.log(this.templateSelection)
      this.temp.marketPrice=row.proMarketPrice
      this.temp.proTitle=row.proTitle
      this.temp.proPriceId=row.proPriceId
      this.temp.sellPrice=row.proSellPrice
    },
    queryGoods(){
      queryGoods(this.temp.proId).then(res=>{
        this.GoodsName=res.data.infoName
        this.data1List=res.data.list
        this.temp.imageUrl=res.data.imageUrl
        this.temp.proCat=res.data.proCat
        this.temp.goodsBrand=res.data.goodsBrand
        this.temp.proCode=res.data.proCode
        console.log(this.data1List)
        if(this.data1List.length===0){
          this.isShow=false
        }else{
          this.isShow=true
        }
        this.templateSelection=null
        this.temp.proPriceId=''
        // this.rowId=''
      })
      
    },
    getList() {
      var params = {
        // pageConvert: true,
        pageNum: this.listQuery.pageNum-1,
        pageSize: this.listQuery.pageSize,
        proCat:this.depValue,
        proCode: this.listQuery.proCode,
        infoName: this.listQuery.infoName,
      }
      getListPage(params).then(response => {
        this.list = response.content
        this.total = response.totalElements
        // Just to simulate the time of the request
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleFilter1() {
      this.detailsQuery.page = 1
      this.detailsSearch()
    },
    detailsSearch(){
      this.handleDetails(this.detailsRow)
    },
    resetTemp() {
      this.temp = {
      }
    },
     handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      // this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.queryGoods()
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDetails(row){
      this.detailsRow=row
      this.detailsVisible=true
      this.datalist=[]
      this.total1=0
      var params = {
        // pageConvert: true,
        pageNum: this.detailsQuery.pageNum-1,
        pageSize: this.detailsQuery.pageSize,
        sex:this.detailsQuery.sex,
        userName: this.detailsQuery.userName,
        nickName: this.detailsQuery.nickName,
        proPriceId: row.proPriceId
        
      }
      getDetailsList(params).then(res=>{
        this.datalist = res.data.content
        this.total1 = res.data.totalElements
        // Just to simulate the time of the request
      })
    },
    handleDel(row){
      this.delId=row.exchangeId
      var params={
        exchangeId:this.delId,
        status:'0'
      }
      delProinfo(params).then(res=>{
        if(res.code==='200'){
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        }
      })
    },
    updateData() {
      this.temp.exchangeAmount=Number(this.temp.exchangeAmount)
      this.temp.eqXkb=Number(this.temp.eqXkb)
      this.temp.infoName=this.GoodsName
      updataForm(this.temp).then(() => {
        this.dialogFormVisible = false
        this.$notify({
          title: '成功',
          message: '修改成功',
          type: 'success',
          duration: 2000
        })
        this.getList()
      })
      
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.temp.exchangeAmount=Number(this.temp.exchangeAmount)
      this.temp.eqXkb=Number(this.temp.eqXkb)
      this.temp.infoName=this.GoodsName
      createForm(this.temp).then(() => {
        this.list.unshift(this.temp)
        this.dialogFormVisible = false
        this.$notify({
          title: '成功',
          message: '添加成功',
          type: 'success',
          duration: 2000
        })
        this.getList()
      })

    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}`
        ? 'ascending'
        : sort === `-${key}`
          ? 'descending'
          : ''
    }
  }
}
</script>
<style  scoped>
  /deep/ .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
